#include <bits/stdc++.h>
using namespace std;
#define int long long
void solve()
{
    int n,m;
    cin>>n>>m;
    int ans=0;
    while(n!=m)
    {
        int x;
        if(n>m)
        {
            x=n/m;
            ans+=4*m*x;
            n-=m*x;
            if(!n)
            {
                cout<<ans<<endl;
                return ;
            }
        }
        else 
        {
            x=m/n;
            ans+=4*n*x;
            m-=n*x;
            if(!m)
            {
                cout<<ans<<endl;
                return ;
            }
        }
        //cout<<ans<<endl;
    }
    if(n==1)ans+=4*m;
    else ans+=4*n;
    //ans+=4*m;
    cout<<ans<<endl;
}
signed main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int T=1;
    //cin>>T;
    while(T--)
    {
       solve();
    }
    return 0;
}